package com;

import java.util.Scanner;
import java.util.ArrayList;


public class CarSystem {

	private static String goodByeInfo = "欢迎再次使用本系统，再见！";
	private static int dayBorrow;
	
	public static void beginSystem() {
		CarSystem.systemWelcome();
		Scanner scanner = new Scanner(System.in);
		try {
			String userCommand = scanner.next();
			switch(userCommand){
	        case "1":
	            CarSystem.getUserInput();
	            break;
	        case "0":
	            System.out.println(goodByeInfo);
	            break;
	        default:
	            System.out.println("输入错误..End running..");
	            System.exit(0);
	            break;
	        }
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Exception thrown  :" + e);
		}
			
		scanner.close();
		
	}
	
	public static void systemWelcome() {
		System.out.println("欢迎使用哒哒租车系统");
		System.out.println("您是否要租车：1是，0否");
	}
	
	public static void getUserInput() {
		int numCarBorrow = 0;
		ArrayList<Car> carList = new ArrayList<Car>(6);
		carList.add(new Sedan(4,500,"奥迪A4"));
        carList.add(new Sedan(4,400,"马自达6"));
        carList.add(new Pickup(4,450,2,"皮卡雪6"));
        carList.add(new Sedan(20,800,"金龙"));
        carList.add(new Truck(400,4,"松花江"));
        carList.add(new Truck(1000,20,"依维河"));
        
        //showList
        for(int i=0, n=1; i < carList.size(); i++,n++) {
        	if(i == 0) {
        		System.out.println("租车列表：");
        		System.out.println("序号	汽车名称		租金			容量");
        	}
        	System.out.print(n + "\t");
        	System.out.print(carList.get(i).getName() + "\t\t");
        	System.out.print(carList.get(i).getPrice() + "元/每天\t\t");
        	switch(carList.get(i).getModel()) {
        	case 0:
        		System.out.println("载人：" + carList.get(i).getPersonNum() + "人");
        		break;
        	case 1:
        		System.out.println("载物：" + carList.get(i).getGoodsNum() + "吨");
        		break;
        	default:
        		System.out.print("载人：" + carList.get(i).getPersonNum() + "人 ");
        		System.out.println("载物：" + carList.get(i).getGoodsNum() + "吨");
        	}
        }
        
        
        System.out.println("输入租车数量：");
        Scanner sr = new Scanner(System.in);
        
        numCarBorrow = sr.nextInt();
        
        int[] carNumList = new int[numCarBorrow];
        for(int i=0; i < numCarBorrow; i++) {
        	System.out.println("请输入第" + (i+1) + "辆车的序号：");
        	if(sr.hasNext()) {
        		carNumList[i] = sr.nextInt();
        	}
        }
        
        System.out.println("输入租车天数：");
        if(sr.hasNext()) {
        	dayBorrow = sr.nextInt();
        }
        sr.close();
        
        //??
        StringBuilder manned = new StringBuilder();
        int numOfManned = 0;
        StringBuilder goods = new StringBuilder();
        int numOfGoods = 0;
        int totalCost = 0;
        
        for(int i=0; i < carNumList.length; i++) {
        	
        	switch(carList.get(carNumList[i]-1).getModel()) {
        	case 0:
        		manned.append("	");
        		manned.append(carList.get(carNumList[i]-1).getName());
        		numOfManned += carList.get(carNumList[i]-1).getPersonNum();
        		break;
        	case 1:
        		goods.append("	");
                goods.append(carList.get(carNumList[i]-1).getName());
                numOfGoods += carList.get(carNumList[i]-1).getGoodsNum();
                break;
        	case 2:
        		manned.append("	");
                manned.append(carList.get(carNumList[i]-1).getName());
                goods.append("	");
                goods.append(carList.get(carNumList[i]-1).getName());
                numOfManned += carList.get(carNumList[i]-1).getPersonNum();
                numOfGoods += carList.get(carNumList[i]-1).getGoodsNum();
                break;
        	default:
        		System.out.println("车类型错误？");
        		System.out.println("错误车型代码：" + carList.get(carNumList[i]-1).getModel());
        		break;
        	}
        	
        	
        	totalCost += carList.get(carNumList[i]-1).getPrice();
        }
        
        //output
        System.out.println("您的账单:\n***载人的车有:");
        System.out.println(manned + "    共载人: " + numOfManned); 
        System.out.println("***载货的车有:\n" + goods + "    共载货 : " + numOfGoods + "吨");
        System.out.println("***租车总价格: " + totalCost * dayBorrow + "元");
	}
}
